package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAddressMapper;
import com.qf.pojo.DtsAddress;
import com.qf.service.DtsAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**  收货地址
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/16 20:25
 */

@Service
public class DtsAddressServiceImpl implements DtsAddressService {

    @Autowired
    DtsAddressMapper addressMapper;
    @Override
    public Map<String, Object> selectAddressByPage(Integer page, Integer limit, String sort, String order, String name, Integer userId) {
        IPage<DtsAddress> iPage = new Page<>(page,limit);
        LambdaQueryWrapper<DtsAddress> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like(DtsAddress::getName,name);
        }
        if (userId!=null && !userId.equals("")){
            wrapper.like(DtsAddress::getUserId,userId);
        }

        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc();
        }else{
            wrapper.orderByAsc();
        }

        iPage = addressMapper.selectPage(iPage, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return data;
    }
}
